package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Admin;
import com.easy.bean.Customer;
import com.easy.bean.Member;
import com.easy.bean.Stylist;
import com.easy.mapper.AdminDao;
import com.easy.mapper.CustomerDao;
import com.easy.mapper.MemberDao;
import com.easy.mapper.StylistDao;
import com.easy.service.LoginService;
import com.easy.util.ImageUtil;
import com.easy.util.PasswordUtil;
import com.easy.util.SYS;
import com.easy.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceimpl implements LoginService {

    @Autowired
    CustomerDao customerDao;
    @Autowired
    AdminDao adminDao;
    @Autowired
    StylistDao stylistDao;
    @Autowired
    private MemberDao memberDao;


    @Override
    public String login(Customer customer) {
        //登录逻辑
        //1.通过用户名查询数据库
        Customer result=customerDao.getByPhone(customer.getPhone());
        if(result==null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password = customer.getPassword();
        String encryptionPassword = result.getPassword();
        if(!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(),"customer");

        return token;
    }

    @Override
    public String adminlogin(Admin admin) {
        //登录逻辑
        //1.通过用户名查询数据库
        Admin result=adminDao.getByUsername(admin.getUsername());
        if(result==null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password = admin.getPassword();
        String encryptionPassword = result.getPassword();
        if(!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(),"admin");

        return token;
    }

    @Override
    public String stylistlogin(Stylist stylist) {
        //登录逻辑
        //1.通过用户名查询数据库
        Stylist result=stylistDao.getByPhone(stylist.getPhone());
        if(result==null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password = stylist.getPassword();
        String encryptionPassword = result.getPassword();
        if(!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("登录异常");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(),"stylist");

        return token;
    }

    @Override
    public Customer logininfo(String token) {
        //获取token中的用户id
        Object id = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
        if(id==null){
            throw new RuntimeException("登录异常");

        }
        Customer customer = customerDao.one(Integer.parseInt(id.toString()));
        return customer;
    }

    @Override
    public Admin adminlogininfo(String token) {
        //获取token中的用户id
        Object id = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
        if(id==null){
            throw new RuntimeException("登录异常");

        }
        Admin admin = adminDao.one(Integer.parseInt(id.toString()));
        return admin;
    }

    @Override
    public Stylist stylistlogininfo(String token) {
        //获取token中的用户id
        Object id = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
        if(id==null){
            throw new RuntimeException("登录异常");

        }
        Stylist stylist = stylistDao.one(Integer.parseInt(id.toString()));
        return stylist;
    }

    @Override
    public boolean changeadminPassword(int adminId, String oldPassword, String newPassword) {
        // 1. 根据ID获取管理员信息
        Admin admin = adminDao.one(adminId);
        if (admin == null) {
            throw new RuntimeException("管理员不存在");
        }

        // 2. 验证旧密码是否正确
        String encryptionPassword = admin.getPassword();
        if (!PasswordUtil.checkPassword(oldPassword, encryptionPassword)) {
            throw new RuntimeException("旧密码错误");
        }

        // 3. 加密新密码
        String newEncryptionPassword = PasswordUtil.getEncryption(newPassword);

        // 4. 更新密码
        Admin updateAdmin = new Admin();
        updateAdmin.setId(adminId);
        updateAdmin.setPassword(newEncryptionPassword);

        int count = adminDao.edit(updateAdmin);
        return count > 0;
    }
    @Scheduled(cron = "0 13 10 * * ?")
    @Override
    @LogMethod(serviceName = "清除服务器冗余文件", recordParam = true, recordReturn = true)
    public void deleteImageUtil(){
        System.out.println("开始执行图片清理任务...");
        List<String> fileList = ImageUtil.getFileList();
        List<String> allImgUrls = memberDao.getAllImgUrls();
        List<String> list = new ArrayList<>();
        for(int i=0;i<allImgUrls.size();i++){
            String s = allImgUrls.get(i);
            s = s.substring(s.lastIndexOf("/") + 1);
            list.add(s);
        }
        for(String imgUrl:fileList){
            if(!list.contains(imgUrl)){
                ImageUtil.delete(imgUrl);
            }
        }
    }


}
